/**
 * 
 */
package com.yunyike.controller.shop;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.exception.BadRequestException;
import com.yunyike.exception.BusinessException;
import com.yunyike.model.Shop;
import com.yunyike.response.Response;
import com.yunyike.service.IMerchandiseService;
import com.yunyike.service.IShopService;

/**
 * @author zhengqiang.shi
 * 2015年11月21日 下午6:15:28
 */
@Controller
@RequestMapping(value=RequestConstant.SHOP)
public class ShopController extends BaseController {

	private Logger log = LoggerFactory.getLogger(ShopController.class);
	
	@Autowired
	private IShopService shopService;
	
	@Autowired
	private IMerchandiseService merchandiseService;
	
	@Value("${itemPageSize}")
	private int itemPageSize;
	
	/**
	 * 店铺首页
	 * @param shopId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.SHOP_FIND+"/{shopId}")
	public void findShopById(@PathVariable String shopId,HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		// response object
		Response res = new Response();
		
		if(StringUtils.isBlank(shopId)){
			log.info("controller findShopById request param not correct.");
			res.getResult().setCode(CommonConstant.NUMBER__1);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__1));
			returnDatas(res, response);
			return;
		}
		
		// 查询店铺信息
		Shop shop = shopService.findShopById(shopId);
		if(shop == null){
			log.info("no shop by shopId:{} ",shopId);
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		}
		
		res.setData(shop);
		returnDatas(res, response);
		return;
	}
	
	/**
	 * 扫描店铺
	 * @param shopId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value=RequestConstant.SHOP_SCAN+"/{shopId}")
	public String scanShopById(@PathVariable String shopId,HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		log.info("controller findShopById...");
		if(StringUtils.isBlank(shopId)){
			log.info("controller findShopById request param not correct.");
			throw new BadRequestException();
		}
		
		// 查询店铺信息
		Shop shop = shopService.findShopById(shopId);
		
		if(shop == null){
			log.info("shopId:"+shopId +"no shop info.");
			return ResponseConstant.SHOP_NOT_FOUND;
					
		}
		
		return ResponseConstant.REDIRECT+RequestConstant.SHOP+RequestConstant.SHOP_FIND+"/"+shop.getShopId();
		
	}
	
	/**
	 * 店首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value=RequestConstant.SHOP_INDEX)
	public String shopIndex(HttpServletRequest request, HttpServletResponse response,Model model){
		
		List<Shop> shops = shopService.findAllShops();
		
		if(shops == null || shops.isEmpty()){
			throw new BusinessException("没有找到任何店铺。");
		}
		
		if(shops.size() == 1){
			Shop shop = shops.get(0);
			log.info("只有一个店铺，shop:{}",shop);
			return ResponseConstant.REDIRECT+RequestConstant.SHOP+RequestConstant.SHOP_FIND+"/"+shop.getShopId();
		}
		
		return null;
	}
	
	/**
	 * 店铺列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.SHOP_LIST)
	public void shopListPost(HttpServletRequest request, HttpServletResponse response){
		
		// response object
		Response res = new Response();
		
		List<Shop> shops;
		try {
			shops = shopService.findAllShops();
		} catch (Exception e) {
			log.error("shopService.findAllShops has error.message:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		
		if(shops == null || shops.isEmpty()){
			log.info("no shops find.");
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		}
		
		res.setData(shops);
		returnDatas(res, response);
		return;
	}
	
}
